﻿using System;
using System.Windows.Forms;
using ExtendLib;
using ExtendLib.Helper;
using InterfaceLib;
using Ivony.Fluent;

namespace SmsBomb
{
    public partial class ConfigAdd : Form
    {
        public int? KeyId { get; set; }
        public ConfigAdd()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            MsgModel model = new MsgModel
            {
                Id = KeyId,
                BaseUrl = this.txtBaseUrl.Text,
                PostUrl = this.txtPostUrl.Text,
                PostData = this.txtPostData.Text,
                RequestType =
                    (ERequestType)Enum.Parse(typeof(ERequestType), this.cbRequestType.SelectedItem.ToString()),
                SuccessFlag = this.txtSuccessFlag.Text,
                Wait = int.Parse(this.txtWait.Text),
                //为空 默认为电脑端
                UseAgent = this.txtUseAgent.Text.IsNullOrEmpty() ? "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" : this.txtUseAgent.Text,
                Status=this.cbStatus.SelectedItem.ToString()
            };
            //为空插入 否则编辑模式
            int r=(KeyId == null ? DbHelperByDapper.InsertDb(model) : DbHelperByDapper.ModifyDb(model));
            if (r > 0)
            {
                this.DialogResult = DialogResult.OK;
                MessageBox.Show("操作成功");
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败");
            }
        }
        /// <summary>
        /// 默认加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfigAdd_Load(object sender, EventArgs e)
        {
            if (KeyId == null)
                return;
            this.labKey.Text = KeyId.ToString();
            var model = DbHelperByDapper.GetModel<MsgModel>(KeyId);
            this.txtBaseUrl.Text = model.BaseUrl;
            this.txtPostUrl.Text = model.PostUrl;
            this.txtPostData.Text = model.PostData;
            this.cbRequestType.SelectedItem = model.RequestType.ToString();
            this.txtSuccessFlag.Text = model.SuccessFlag;
            this.txtUseAgent.Text = model.UseAgent;
            this.txtWait.Text = model.Wait.ToString();
            this.cbStatus.SelectedItem = model.Status;
        }
    }
}
